package com.jalarbee.web.jsf.controller;

import com.jalarbee.liquid.services.business.Service;
import com.jalarbee.liquid.services.service.ServiceService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Abdoulaye <b>Ikhnaton</b> Diallo
 */
@Named
@RequestScoped
public class ServiceController implements Serializable {
    
    @Inject
    private ServiceService serviceService;
    private Service service;
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceController.class);
    List<Service> services = new ArrayList<Service>();
    private Set<Integer> keys = new LinkedHashSet<Integer>();
    
    public ServiceController() {
    }
    
    public void initService() {
        if (service == null) {
            service = new Service();
            
        }
    }
    
    public Service getService() {
        initService();
        return service;
    }
    
    public String add() {
        try {
            serviceService.create(service);
            return Navigation.SUCCESS;
        } catch (Exception e) {
            return Navigation.FAILURE;
        }
    }
    
    public List<Service> getServices() {
        try {
            services = serviceService.findAll(null);
            for (Service s : services) {
                keys.add(s.getId());
            }
            return services;
        } catch (Exception exception) {
            LOGGER.error(exception.getMessage(), exception);
            return null;
        }
    }
    
    public String update() {
        try {
            serviceService.update(service);
            return Navigation.SUCCESS;
        } catch (Exception exception) {
            return Navigation.FAILURE;
        }
    }
    
    public Set<Integer> getKeys() {
        return keys;
    }

    public void setService(Service service) {
        this.service = service;
    }
    
}
